package com.glg.springmvcdemo2;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtils {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {
        Person person = new Person();
        person.setId(4444);
        person.setName("Yechiel");
        person.setPassword("3018");

        //对象转为 JSON 字符串
        String jsonStr = objectMapper.writeValueAsString(person);
        System.out.println(jsonStr);

        //JSON 字符串转为对象
        Person p = objectMapper.readValue(jsonStr,Person.class);
        System.out.println("转换的对象id: "+p.getId()+", name: "+p.getName()+", password: "+p.getPassword());
    }
}
